package com.supercube.sport.yoga.controller;

import com.supercube.common.controller.BaseController;
import com.supercube.common.utils.DateUtils;
import com.supercube.common.utils.QiNiuUtils;
import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.domain.MemberBodyInfoDTO;
import com.supercube.sport.yoga.service.MemberBodyInfoService;
import com.supercube.sport.yoga.utils.ResultInfo;
import com.supercube.sport.yoga.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

import static com.supercube.sport.yoga.controller.CommonController.isFileAllowed;

@RestController
@RequestMapping("/memberBodyInfoController")
@Api(tags = "会员体测信息")
public class MemberBodyInfoController extends BaseController {

    @Autowired
    MemberBodyInfoService memberBodyInfoService;

    @ApiOperation(value = "保存会员体测基本信息-带文件", notes = "保存会员体测基本信息-带文件,file(String)：文件")
    @PostMapping("/saveMemberBodyInfoForFile")
    public ResultInfo saveMemberBodyInfoForFile(@RequestParam("file") MultipartFile file, String memberId) {
        boolean upload = false;
        String originalFilename = file.getOriginalFilename();
        int dotPos = originalFilename.lastIndexOf(".");
        if (dotPos < 0) {
            return ResultInfo.failed("上传体侧信息失败！");
        }
        String suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        // 判断是否是合法的文件后缀
        if (!isFileAllowed(suffix)) {
            return ResultInfo.failed("上传体侧信息失败！");
        }
        String fileName = DateUtils.getYear() + "/" + DateUtils.getMonth() + "/" + randomUUID() + "." + suffix;

        try {
            upload = QiNiuUtils.upload(file.getBytes(), fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (upload) {
            MemberBodyInfoDTO dto = new MemberBodyInfoDTO();
            dto.setType("2");
            dto.setBodyUrl(fileName);
            dto.setId(randomUUID());
            dto.setMemberId(memberId);
            UserUtils.setRecordBase(dto);
            memberBodyInfoService.save(dto);
            return ResultInfo.success(fileName, "上传体侧信息成功！");
        }
        return ResultInfo.failed("上传体侧信息失败！");
    }

    /**
     * 保存会员体测基本信息
     *
     * @param dto
     * @return
     */
    @ApiOperation(value = "保存会员体测基本信息", notes = "保存会员体测基本信息")
    @PostMapping("/save")
    public ResultInfo saveMemberBodyInfo(@RequestBody MemberBodyInfoDTO dto) {
        dto.setType("1");// 不带图
        dto.setId(randomUUID());
        UserUtils.setRecordBase(dto);
        memberBodyInfoService.save(dto);
        return ResultInfo.success("保存成功");
    }

    @ApiOperation(value = "修改会员体测基本信息", notes = "修改会员体测基本信息")
    @PostMapping("/update")
    public ResultInfo updateMemberBodyInfo(@RequestBody MemberBodyInfoDTO dto) {
        dto.setLastModifyUser(getUserIdStr());
        dto.setLastModifyTime(TimeUtils.getCurrentTime());
        dto.setType("1");
        memberBodyInfoService.update(dto);
        return ResultInfo.success("修改成功");
    }

    @ApiOperation(value = "删除会员体测基本信息", notes = "删除会员体测基本信息")
    @PostMapping("/delete")
    public ResultInfo deleteMemberBodyInfoById(@RequestParam("id") String id) {
        MemberBodyInfoDTO bodyInfoDTO = memberBodyInfoService.queryMemberBodyInfo(id);
        int deleteFileFromQiNiu = QiNiuUtils.deleteFileFromQiNiu(bodyInfoDTO.getBodyUrl());
        if (deleteFileFromQiNiu>0) {
            memberBodyInfoService.deleteMemberBodyInfoById(id);
            return ResultInfo.success("删除成功");
        }
        return ResultInfo.failed("删除失败");
    }
}
